package com.qf.weekwork.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.weekwork.dao.AdminUserMapper;
import com.qf.weekwork.entity.AdminUser;
import com.qf.weekwork.service.UserService;
import com.qf.weekwork.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public AdminUser selectAdminUserByName(String name){
        AdminUser adminUser = new AdminUser();
        adminUser.setEmail(name);
        AdminUser adminUser1 = adminUserMapper.selectOne(adminUser);
        return adminUser1;
    }

    @Override
    public PageInfo<AdminUser> selectAll(PageVo pageVo, AdminUser adminUser) {
        Example example = new Example(AdminUser.class);
        Example.Criteria criteria = example.createCriteria();
        if(adminUser.getStatus() != null){
            criteria.andEqualTo("status",adminUser.getStatus());
        }else if(adminUser.getId() != null){
            criteria.andEqualTo("id",adminUser.getId());
        }
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<AdminUser> users = adminUserMapper.selectByExample(example);
        PageInfo<AdminUser> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public Boolean add(AdminUser adminUser) {
        int insert = adminUserMapper.insert(adminUser);
        return insert > 0 ? true: false;
    }

    @Override
    public Boolean del(Integer[] ids) {
        Example example = new Example(AdminUser.class);
        Example.Criteria criteria = example.createCriteria();
        for(Integer id : ids){
            criteria.orEqualTo("id",id);
        }
        int i = adminUserMapper.deleteByExample(example);
        return i>0? true:false;

    }

    @Override
    public Boolean update(AdminUser adminUser) {
        int i = adminUserMapper.updateByPrimaryKey(adminUser);
        return i>0? true:false;
    }
}
